#include "log.h"
#include "comm_param.h"
void crq_set_cmdId(comm_req_param_t* crq, int cmdId, int status, int code, char* msg)
{
    crq->cmdId = cmdId;
    crq->status = status;
    crq->code = code;
    if (msg)
        strcpy(crq->msg, msg);
}
void crq_reset_cmdId(comm_req_param_t* crq)
{
    crq->cmdId = 0;
    crq->status = 0;
    crq->code = 0;
    bzero(crq->msg, sizeof(crq->msg));
}
void crq_clone(comm_req_param_t* dist, comm_req_param_t* comm_req)
{
    memcpy(dist, comm_req, sizeof(comm_req_param_t));
}

void crq_set_devkey(comm_req_param_t* crq, char* key)
{
    strcpy(crq->developerKey, key);
}
void crq_set_mac(comm_req_param_t* crq, char* mac)
{
    strcpy(crq->mac, mac);
}
void crq_set_currentTime(comm_req_param_t* crq)
{
    struct tm *p, tm_p;
    time_t timep;

    time(&timep);
    localtime_r(&timep, &tm_p);
    p = &tm_p;
    sprintf(crq->currentTime, "%d-%02d-%02d %02d:%02d:%02d",
        1900 + p->tm_year, 1 + p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
}
void crq_set_clientType(comm_req_param_t* crq, char* clientType)
{
    strcpy(crq->clientType, clientType);
}
void crq_set_collectType(comm_req_param_t* crq, int collectType)
{
    crq->collectType = collectType;
}

void crq_set_probeId(comm_req_param_t* crq, char* probeId)
{
    strcpy(crq->probeId, probeId);
}
void crq_set_version(comm_req_param_t* crq, char* version)
{
    strcpy(crq->version, version);
}

cJSON* crq2cjson(comm_req_param_t* crq, cJSON* root)
{
    cJSON* _root;

    if (!root)
        _root = cJSON_CreateObject();
    else
        _root = root;

    // cJSON_AddStringToObject(_root, "developerKey", crq->developerKey);
    cJSON_AddStringToObject(_root, "mac", crq->mac);
    cJSON_AddStringToObject(_root, "currentTime", crq->currentTime);
    cJSON_AddStringToObject(_root, "clientType", crq->clientType);
    cJSON_AddStringToObject(_root, "msg", crq->msg);
    cJSON_AddStringToObject(_root, "probeId", crq->probeId);
    cJSON_AddStringToObject(_root, "probeVersion", crq->version);
    // cJSON_AddNumberToObject(_root, "collectType", crq->collectType);
    cJSON_AddNumberToObject(_root, "cmdId", crq->cmdId);
    cJSON_AddNumberToObject(_root, "status", crq->status);
    cJSON_AddNumberToObject(_root, "code", crq->code);
    return _root;
}

void load_probe_version(char* file, char* buffer, int len)
{
    FILE* fp;

    fp = fopen(file, "r");
    if (!fp) {
        log_syserr("open %s failed.", file);
        return;
    }
    fgets(buffer, len, fp);
    fclose(fp);
}
void save_probe_version(char* file, char* buffer, int len)
{
    FILE* fp;

    fp = fopen(file, "w");
    fwrite(buffer, len, 1, fp);
    fclose(fp);
}

int check_platform_resp(cJSON* root)
{
    comm_resp_param_t comm_resp;
    cJSON* json = NULL;

    if (!root) {
        return 0;
    }

    cJSON_getvalue(root, "status", cJSON_String, (void*)comm_resp.status);
    cJSON_getvalue(root, "code", cJSON_String, (void*)comm_resp.code);
    cJSON_getvalue(root, "msg", cJSON_String, (void*)comm_resp.msg);

    if (strcmp(comm_resp.status, "1") != 0) {
        log_error("platform resp msg:%s", comm_resp.msg);
        return 0;
    }

    return 1;
}